import enum
from datetime import datetime

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum
from sqlalchemy.orm import relationship

from database import Base, engine


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True, index=True, comment="user_id", autoincrement=True)
    username = Column(String(255), unique=True, index=True, comment="user_username")
    password = Column(String(255), comment="user_password")
    email = Column(String(255), unique=True, index=True, comment="user_email")
    photos = Column(String(255), comment="user_photos")
    create_time = Column(DateTime, default=datetime.now, comment="user_create_time")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="user_update_time")

    solved_solutions = relationship("Solution", back_populates="solver", lazy="joined")
    published_questions = relationship("Question", back_populates="publisher", lazy="joined")


class Question(Base):
    __tablename__ = 'questions'

    id = Column(Integer, primary_key=True, index=True, comment="question_id", autoincrement=True)
    title = Column(String(255), comment="question_title")
    content = Column(String(1000), comment="question_content")
    type = Column(String(255), comment="question_type")
    difficulty = Column(String(255), comment="question_difficulty")
    create_time = Column(DateTime, default=datetime.now, comment="question_create_time")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="question_update_time")

    # 外键：发布者（老师）ID
    publisher_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="发布者ID（老师）")

    # 关联关系：
    # ① 发布该题目的老师
    publisher = relationship("User", back_populates="published_questions")

    # ② 该题目的所有解答
    solutions = relationship("Solution", back_populates="question", lazy="joined")

    # ③ 该题目的所有标签
    tags = relationship("Tag", secondary="question_tag", back_populates="questions")

# 定义解答状态的枚举（可选值限制）
class SolutionState(enum.Enum):
    START = "尝试过"
    FINISH = "已完成"
    NOTSTART = "未开始"

class Solution(Base):
    __tablename__ = 'solutions'

    id = Column(Integer, primary_key=True, index=True, comment="解答的ID", autoincrement=True)
    content = Column(String(1000), comment="解题的内容")
    state = Column(Enum(SolutionState), comment="解答状态")
    create_time = Column(DateTime, default=datetime.now, comment="解答的创造时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="解答问题的更新时间")

    # 外键：解答者（学生）ID
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, comment="学生ID")
    # 外键：题目ID
    question_id = Column(Integer, ForeignKey("questions.id", ondelete="CASCADE"), nullable=False, comment="题目ID")
    # 关联关系：
    # ① 解答该题目的学生
    solver = relationship("User", back_populates="solved_solutions")
    # ② 该解答对应的题目
    question = relationship("Question", back_populates="solutions")

    # ③ 该解答的所有标签
    tags = relationship("Tag", secondary="solution_tag", back_populates="solutions")


class QuestionTag(Base):
    __tablename__ = 'question_tag'

    question_id = Column(Integer, ForeignKey("questions.id", ondelete="CASCADE"), primary_key=True, comment="question_id")
    tag_id = Column(Integer, ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True, comment="tag_id")

class SolutionTag(Base):
    __tablename__ = 'solution_tag'

    solution_id = Column(Integer, ForeignKey("solutions.id", ondelete="CASCADE"), primary_key=True, comment="solution_id")
    tag_id = Column(Integer, ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True, comment="tag_id")

class Tag(Base):
    __tablename__ = 'tags'

    id = Column(Integer, primary_key=True, index=True, comment="tag_id", autoincrement=True)
    name = Column(String(255), unique=True, index=True, comment="tag_name")
    create_time = Column(DateTime, default=datetime.now, comment="tag_create_time")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="tag_update_time")

    # 关联关系：
    # ① 该标签下的所有题目
    questions = relationship("Question", secondary="question_tag", back_populates="tags")

    # 该标签下的所有解答
    solutions = relationship("Solution", secondary="solution_tag", back_populates="tags")


Base.metadata.create_all(bind=engine)